package com.example.demo.client.view;

import com.example.demo.client.common.CommonRequest;
import com.example.demo.client.common.ViewKey;
import com.example.demo.ds.MyList;
import com.example.demo.server.common.APPUtils;
import com.example.demo.server.common.CommonResponse;
import com.example.demo.server.controller.UserController;
import com.example.demo.server.model.UserInfo;


public class LoginView implements View {

    private UserController userController = new UserController();

    /**
     *  展示页面
     */
    public void showPage(CommonRequest request) {
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("+--------------------------------------------------------------------------------+");
        System.out.println("| [未登录]                                     主页（list）    注册（register）      |");
        System.out.println("+--------------------------------------------------------------------------------+");
        System.out.println("|                                                                                |");
        System.out.println("|             +———————————————————————————————————————————————————+              |");
        System.out.println("|             |                     [login]                       |              |");
        System.out.println("|             |                                                   |              |");
        System.out.println("|             |      username:_______________________________     |              |");
        System.out.println("|             |      password:_______________________________     |              |");
        System.out.println("|             |                                                   |              |");
        System.out.println("|             +———————————————————————————————————————————————————+              |");
        System.out.println("|                                                                                |");
        System.out.println("|________________________________________________________________________________|");
    }

    @Override
    public CommonRequest jump(CommonRequest request) {
        showPage(request);
        String command = APPUtils.input();
        MyList<String> myList = APPUtils.handleCommand(command);
        if(myList.size() == 3 && myList.get(0).equals(ViewKey.LOGIN)) {
            // 代表信息正确
            if(!myList.get(1).trim().equals("") && !myList.get(2).trim().equals("")) {
                // 发送请求给服务器
                UserInfo userInfo = new UserInfo();
                userInfo.setUsername(myList.get(1).trim());
                userInfo.setPassword(myList.get(2).trim());
                CommonResponse commonResponse = userController.login(userInfo);
                if(commonResponse.getCode() == 200) {
                    APPUtils.printPopUp("Login Success!");
                    return CommonRequest.createRequest(ViewKey.LIST);
                }else {
                    APPUtils.printPopUp("ERROR: " + commonResponse.getMsg());
                    return CommonRequest.createRequest(ViewKey.LOGIN);
                }

            }
        }else if(myList.size() == 1) {
            String view = myList.get(0);
            CommonRequest commonRequest = APPUtils.checkJump(view, ViewKey.LOGIN, ViewKey.REGISTER, ViewKey.LIST);
            if(commonRequest != null) {
                return commonRequest;
            }
        }
        APPUtils.printPopUp("ERROR: Please Again!");
        return CommonRequest.createRequest(ViewKey.LOGIN);
    }
}
